<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>评论管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/x-admin/font.css">
    <link rel="stylesheet" href="/static/css/x-admin/xadmin.css">
    <link rel="stylesheet" href="/static/css/admin-index.css">
</head>
<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>控制台</a>
        <a>文章管理</a>
        <a>目录管理</a>
        <a><cite>编辑目录</cite></a>
      </span>
    <a class="layui-btn layui-btn-sm layui-btn-radius" style="line-height:1.6em;margin-top:4px;float:right"
       href="javascript:location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-anim layui-anim-scale x-body">
    <form id="cateForm" method="post" class="layui-form">
        <div class="layui-hide">
            <input type="hidden" name="cateType" value="<#if cateType??>${cateType}</#if>"/>
            <input type="hidden" name="cateId" value="<#if category??>${category.getCateId()}</#if>"/>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md3">
                <label for="cateName" class="layui-form-label-col form-title">名称：</label>
                <div id="cateName" class="layui-input-block admin-input-block">
                    <input id="cateName" type="text" name="cateName" class="layui-input" size="40"
                           maxlength="50" value="<#if category??>${category.getCateName()}<#else>未命名</#if>">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md3">
                <label for="cateOrder" class="layui-form-label-col form-title">排序：</label>
                <div class="layui-input-block admin-input-block">
                    <input id="cateOrder" type="number" min="0" max="99999999"
                           value="<#if category??>${category.getCateOrder()}<#else>0</#if>" name="cateOrder"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md3">
                <label class="layui-form-label-col form-title" for="cate">父目录：</label>
                <div class="layui-input-block admin-input-block">
                    <select id="cate" name="categoryId"
                            lay-verify="" lay-filter="cate-select">
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-md5">
                <label class="layui-form-label-col form-title" for="cateIntro">目录描述：</label>
                <div class="layui-input-block admin-input-block">
                    <textarea name="cateIntro" class="layui-textarea" id="cateIntro"
                              rows="5"><#if category??><#if category.getCateIntro()??>${category.getCateIntro()}</#if></#if></textarea>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block admin-input-block">
                <button lay-submit type="button" class="layui-btn">立即提交</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="/static/js/jquery/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/x-admin/xadmin.js"></script>
<script>
    // 初始化分类目录
    function loadCategory() {
        var options = "<option value=\"0\">请选择</option>\n";
        $.ajax({
            url: "/admin/cate/cateAll",
            type: "post",
            dataType: "json",
            success: function (data) {
                buildOptions(data['data'].categoryList);
                var category = $("#cate");
                category.html(options);
                <#if category??>
                category.val("#{category.getCateParentId()}");
                </#if>
                renderFrom();
            }, error: function () {
                layer.msg("初始化分类目录失败");
            }
        });

        function buildOptions(list) {
            if (list === null || list.length < 0) {
                return;
            }
            for (var i = 0; i < list.length; i++) {
                var cate = list[i];
                var space = "";
                for (var j = 1; j < cate.index; j++) {
                    space += "&nbsp;&nbsp;";
                }
                <#if category??>
                    var id = ${category.getCateId()};
                    if (id !== cate.cateParentId && id !== cate.cateId) {
                        options += "<option value=\"" + cate.cateId + "\">" + space + "LV" + cate.index + "." + cate.cateName + "</option>\n";
                    }
                </#if>
                if (cate.children.length > 0) {
                    buildOptions(cate.children);
                }
            }
        }
    }

    // 重新加载表单
    function renderFrom() {
        layui.use('form', function () {
            var form = layui.form;
            form.render();
        });
    }

    loadCategory();

    layui.use('form', function () {
        var form = layui.form;
        form.on('submit', function (data) {
            var cateIntro = data.field.cateIntro;
            var cateName = data.field.cateName;
            var cateOrder = data.field.cateOrder;
            var cateType = data.field.cateType;
            var cateParentId = data.field.categoryId == null ? 0 : data.field.categoryId;
            var cateId = data.field.cateId;
            if (cateType === 'newCate') {
                $.ajax({
                    url: "/admin/cate/saveCategoryInfo",
                    data: {
                        cateIntro: cateIntro,
                        cateName: cateName,
                        cateOrder: cateOrder,
                        cateParentId: cateParentId
                    },
                    type: "POST",
                    success: function (data) {
                        layer.open({
                            content: data.message,
                            yes: function (index) {
                                if (data.code === 2008) {
                                    window.location.href = "/admin/cate/showCateAll";
                                    layer.close(index);
                                }
                            }
                        });
                    }, error: function () {
                        layer.msg("目录保存失败");
                    }
                })
            }
            if (cateType === 'updateCate') {
                $.ajax({
                    url: "/admin/cate/updateCategoryInfo",
                    data: {
                        cateIntro: cateIntro,
                        cateName: cateName,
                        cateOrder: cateOrder,
                        cateParentId: cateParentId,
                        cateId: cateId
                    },
                    type: "POST",
                    success: function (data) {
                        layer.open({
                            content: data.message,
                            yes: function (index) {
                                if (data.code === 2009) {
                                    window.location.href = "/admin/cate/showCateAll";
                                    layer.close(index);
                                }
                            }
                        });
                    }, error: function () {
                        layer.msg("目录更新失败");
                    }
                })
            }
        });
    });
</script>
</body>
</html>